from datetime import datetime, date, timedelta
from flask import current_app
from apps.api.models import User, UserRecord
import shortuuid
from apps.common.models import *
from apps.mall.models import *
from exit import db
import config
from decimal import *
from utils import scheduler, api_util


def trade_time(x):  # 时间调度执行函数
    with scheduler.app.app_context():

            db.session.commit()
            print('这是一个定时任务完成时间', datetime.now())


def time_task(buying_id):  # 时间调度器
    job = {
        'id': shortuuid.uuid(),  # 任务的唯一ID，不要冲突
        'func': 'trade_time',  # 执行任务的function名称
        'args': str(buying_id),  # 如果function需要参数，就在这里添加  现在没用
    }
    result = current_app.apscheduler.add_job(func=__name__ + ':' + job['func'], args=[str(buying_id)],
                                             next_run_time=datetime.now() + timedelta(minutes=5), id=job['id'],
                                             max_instances=1000, misfire_grace_time=3)

    # weeks=0 | days=0 | hours=0 | minutes=0 | seconds=0,

    print(result)
    print(current_app.apscheduler.get_jobs())
    return result
